import axios from "axios";
import { Notify } from "vant"; //vant顶部提示
import { getToken } from "./auth"; //取cookie
// 配置全局请求域名和超时时间
const instance = axios.create({
  // baseURL:"http://lanlianhua.work:3009",// 请求的域名前缀
  baseURL: "http://192.168.31.27:3000", // 请求的域名前缀
});

// 全局请求拦截
instance.interceptors.request.use(
  function (config) {
    //判断本地localStorage是否有token
    //   if (localStorage.getItem("token")) {
    //     config.headers.authorization = `Bearer ${localStorage.getItem("token")}`;
    //   }
    //判断本地cookie是否有token
    // if (getToken()) {
    //   config.headers.authorization = `Bearer ${getToken()}`;
    // }
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);

// 全局响应拦截
instance.interceptors.response.use(
  function (response) {
    //   console.log(response);

    return response; // 这是响应成功返回的信息
  },
  function (error) {
    if (error.response.status == 401) {
      Notify({ type: "warning", message: "未授权，请先登录" });
      // 直接打回登录页面
      window.location.href = "#/login";
    }
    return Promise.reject(error);
  }
);
// 封装get请求
export const get = (url, params) => instance.get(url, { params });

// 封装post请求
export const post = (url, data) => instance.post(url, data);

// 删除
export const del = (url, params) => instance.delete(url, { params });
export default instance;
